package org.example;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import org.apache.log4j.Logger;
import org.example.Command.CommonMessage;
import org.example.Handler.MainClientHandler;
import org.example.Handler.RecvCommandClientHandler;
import org.example.Handler.SendCommandClientHandler;
import org.example.protocol.MessageCodec;
import org.example.protocol.ProtocolFrameDecoder;

import java.util.concurrent.CyclicBarrier;

import static org.example.Constant.clientDirectory;

public class Client {
    //用于控制客户端输入
    public static CyclicBarrier WAIT_FOR_INPUT = new CyclicBarrier(2);
    public static Logger log = Logger.getLogger(Client.class);
    public static void main(String[] args) {
        //初始化文件夹之类
        init();
        NioEventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.channel(NioSocketChannel.class);
            bootstrap.group(group);
            bootstrap.option(ChannelOption.TCP_NODELAY, true);
            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new ProtocolFrameDecoder());
                    ch.pipeline().addLast(new MessageCodec());
                    ch.pipeline().addLast(new IdleStateHandler(0, 3, 0));
                    ch.pipeline().addLast(new ChannelDuplexHandler() {
                        @Override
                        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                            IdleStateEvent event = (IdleStateEvent) evt;
                            if (event.state() == IdleState.WRITER_IDLE) {
                                ctx.writeAndFlush(new CommonMessage("ping"));
                            }
                        }
                    });
                    ch.pipeline().addLast("client sendCommandHandler", new SendCommandClientHandler());
                    ch.pipeline().addLast("client recvCommandHandler", new RecvCommandClientHandler());
                    ch.pipeline().addLast("client handler", new MainClientHandler());
                }
            });
            Channel channel = bootstrap.connect("localhost", 8082).sync().channel();

            channel.closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }
    }

    private static void init() {
        if (!clientDirectory.exists()) {
            if (!clientDirectory.mkdir()) {
                System.out.println("创建失败");
            }
        }
    }


}
